/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

import com.mol.market.R;
import com.mol.market.common.network.ApiAsyncTask;
import com.mol.market.common.widget.LoadingDrawable;

/**
 * @author Chaos	
 * @Description TODO
 * @date 2012-12-26 
 */
public abstract class ListFragmentWrapper extends AbsListViewFragment
                    implements OnClickListener,ApiAsyncTask.ApiRequestListener {

    @Override
    protected View initLayout(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return getCommonList(inflater);
    }

    private ProgressBar mLoading;
    private View        mNoNetWork;
    private Button      mRetry;
    @Override
    protected View initEmptyView(LayoutInflater inflater, View ancestor,
            Bundle savedInstanceState) {
        View emptyView = ancestor.findViewById(R.id.loading);
        mLoading = (ProgressBar) emptyView.findViewById(R.id.progressbar);
        mLoading.setIndeterminateDrawable(new LoadingDrawable(getActivity()));
        mNoNetWork = emptyView.findViewById(R.id.no_network);
        mRetry     = (Button) mNoNetWork.findViewById(R.id.btn_no_network);
        mRetry.setOnClickListener(this);
        emptyView.setVisibility(View.VISIBLE);
        mLoading.setVisibility(View.VISIBLE);
        mNoNetWork.setVisibility(View.GONE);
        return emptyView;
    }
    
    /**
     * 获得通用的ListView，包含Loading和EmtpyView。
     * @param  LayoutInflater
     * @return 包含ListView的ViewGroup 
     */
    protected ViewGroup getCommonList(LayoutInflater inflater){
        return (ViewGroup) inflater.inflate(R.layout.common_list_view_new_loading, null);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn_no_network){
            mLoading.setVisibility(View.VISIBLE);
            mNoNetWork.setVisibility(View.GONE);
            doLoad();
        }
    }
    
    protected abstract void doLoad();
    
    @Override
    public void onStart() {
        doloadIfNeed();
        super.onStart();
    }
    
    protected void doloadIfNeed(){
        if(getAdapter() == null || getAdapter().getCount() == 0){
            doLoad();
        }
    }
    
    @Override
    protected void dealloc() {
        this.mLoading   = null;
        this.mRetry     = null;
        this.mNoNetWork = null;
        super.dealloc();
    }
    
    @Override
    public void onSuccess(int method, Object obj) {
        if(filter(method)){
            return ;
        }
    }

    @Override
    public void onError(int method, int statusCode) {
        if(filter(method)){
            return ;
        }
        if (mLoading != null) {
            mLoading.setVisibility(View.GONE);
        }
        mNoNetWork.setVisibility(View.VISIBLE);
    }
    
    /**
     * 过滤器，根据请求不同，决定是否对Adapter进行操作。
     * {@link #onSuccess(int, Object)} and {@link #onError(int, int)}
     * <br>
     * 默认不过滤
     */
    protected boolean filter(int method){
        return false;
    }
    
    @Override
    protected View initHeaderView(LayoutInflater inflater) {
        return null;
    }
    
    @Override
    protected View initFooterView(LayoutInflater inflater) {
        return null;
    }
}
